//
//  LXAccurateFindCarViewController.m
//  lixing
//
//  Created by bancheng on 2022/6/22.
//

#import "LXAccurateFindCarViewController.h"
#import "LXFindCarCollectionViewCell.h"
#import "LXFindCarModel.h"
#import "LXCarLeaseApplyViewController.h"
@interface LXAccurateFindCarViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic) UICollectionView *collectionView;
@property (nonatomic) NSMutableArray *data;
// 是否选择油车,选择油车时要隐藏续航公里
@property (nonatomic) BOOL isSelOilCar;

@end

@implementation LXAccurateFindCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self layoutCustomNav:self.view title:@"补车提醒"];
    
    self.data = [NSMutableArray array];
 
    [self loadData];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.headerReferenceSize = CGSizeMake(ScreenWidth, 40);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, kViewTopHeight, ScreenWidth, ScreenHeight - kViewTopHeight - 60) collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = UIColor.whiteColor;
    self.collectionView = collectionView;
    [self.view addSubview:collectionView];
    


    collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([LXFindCarCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([LXFindCarCollectionViewCell class])];
    [collectionView registerClass:[UICollectionReusableView  class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class])];
    [collectionView registerClass:[UICollectionReusableView  class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([UICollectionReusableView class])];

    
    UIButton *submitBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    submitBtn.frame = CGRectMake(20, kScreenHeight - 58, ScreenWidth - 40, 48);
    [submitBtn setTitle:@"一键补车" forState:(UIControlStateNormal)];
    submitBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [submitBtn addCorner:4];
    submitBtn.backgroundColor = ColorWith3C6CFC;
    [submitBtn addTarget:self action:@selector(submitAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:submitBtn];
    
    
}
- (void)loadData{
    
    
    [MSNetworkManager getRevertUrl:[DOMAIN_URL stringByAppendingString:GET_GetFindCarData] params:@{} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        // 车辆品牌'
        
        LXFindCarModel *carBrandModel = [LXFindCarModel mj_objectWithKeyValues:@{
            @"title":@"车辆品牌",
            @"child":@[
                @{
                    @"label":[NSString stringWithFormat:@"%@ %@",self.model.carBrand, self.model.carMode],
                    @"isSel":@YES,
                },
            ]
        }];
        [self.data addObject:carBrandModel];
        
        
        // 能源类型
        LXFindCarModel *energModel = [[LXFindCarModel alloc] init];
        if ([resonseObj objectForKey:@"energTypes"]) {
            NSArray *energTypes = resonseObj[@"energTypes"];
            NSMutableArray *ary = [NSMutableArray array];
            
            for (int i = 0; i < energTypes.count; i++) {
                NSDictionary *dic = energTypes[i];
                LXFindCarListModel *model = [LXFindCarListModel mj_objectWithKeyValues:dic];
                if ([model.value intValue] == self.model.energeType) {
                    model.isSel = YES;
                }
               
                [ary addObject:model];
            }
            energModel.title = @"能源类型";
            energModel.type = 1;
            energModel.child = ary;
            
        }
        [self.data addObject:energModel];
        
        // 车辆续航
        LXFindCarModel *enduranceModel = [[LXFindCarModel alloc] init];
        if ([resonseObj objectForKey:@"endurance"]) {
            NSArray *endurance = resonseObj[@"endurance"];
            NSMutableArray *ary = [NSMutableArray array];
            for (int i = 0; i < endurance.count; i++) {
                NSDictionary *dic = endurance[i];
                LXFindCarListModel *model = [LXFindCarListModel mj_objectWithKeyValues:dic];
                if ([model.value isEqualToString:self.model.endurance]) {
                    model.isSel = YES;
                }
                [ary addObject:model];
            }
    
            
            enduranceModel.title = @"车辆续航(公里)";
            enduranceModel.type = 2;
            enduranceModel.child = ary;
            
        }
        [self.data addObject:enduranceModel];
        
        
        // 运行品台
        LXFindCarModel *platModel = [[LXFindCarModel alloc] init];
        if ([resonseObj objectForKey:@"platforms"]) {
            NSArray *platforms = resonseObj[@"platforms"];
            NSMutableArray *ary = [NSMutableArray array];
            for (int i = 0; i < platforms.count; i++) {
                NSDictionary *dic = platforms[i];
                LXFindCarListModel *model = [LXFindCarListModel mj_objectWithKeyValues:dic];
                if ([model.value intValue] == [self.model.runPlatfom intValue]) {
                    model.isSel = YES;

                }
                [ary addObject:model];
            }
    
            
            platModel.title = @"运行平台";
            platModel.type = 3;
            platModel.child = ary;
            
        }
        [self.data addObject:platModel];
        
        // 车辆类型
        LXFindCarModel *typeModel = [[LXFindCarModel alloc] init];
        if ([resonseObj objectForKey:@"carTypes"]) {
            NSArray *catType = resonseObj[@"carTypes"];
            NSMutableArray *ary = [NSMutableArray array];
            for (int i = 0; i < catType.count; i++) {
                NSDictionary *dic = catType[i];
                LXFindCarListModel *model = [LXFindCarListModel mj_objectWithKeyValues:dic];
                if ([model.value intValue] == [self.model.carType intValue]) {
                    model.isSel = YES;

                }
                [ary addObject:model];
            }
    
            
            typeModel.title = @"运行平台";
            typeModel.type = 3;
            typeModel.child = ary;
            
        }
        [self.data addObject:typeModel];
      
      
        
        // 车辆租金
        LXFindCarModel *rentsModel = [[LXFindCarModel alloc] init];
        if ([resonseObj objectForKey:@"rents"]) {
            NSArray *rents = resonseObj[@"rents"];
            NSMutableArray *ary = [NSMutableArray array];
            for (int i = 0; i < rents.count; i++) {
                NSDictionary *dic = rents[i];
                LXFindCarListModel *model = [LXFindCarListModel mj_objectWithKeyValues:dic];
                if ([model.label isEqualToString:self.model.floorPrice]) {
                    model.isSel = YES;
                }
                [ary addObject:model];
            }
            rentsModel.title = @"车辆租金";
            rentsModel.type = 4;
            rentsModel.child = ary;
            
        }
        [self.data addObject:rentsModel];
        
        
        // 油车
        if (self.model.energeType == 1) {
            self.isSelOilCar = YES;
        }
        [self.collectionView reloadData];
        
        
        
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        
    } withShowHud:YES];
    
    
    
    
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.data.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    LXFindCarModel *model = self.data[section];
    return model.child.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LXFindCarModel *model = self.data[indexPath.section];
    LXFindCarListModel *mm = model.child[indexPath.row];
    LXFindCarCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LXFindCarCollectionViewCell class]) forIndexPath:indexPath];
    
    cell.contentLab.text = mm.label;
    
//    if ([mm.typeCode isEqualToString:@"ENERGE_TYPE"]) {
//        cell.contentLab.text = mm.label;
//    }
//    else {
//        NSString *str  = [mm.value stringByReplacingOccurrencesOfString:@"公里" withString:@""];
//        cell.contentLab.text = str;
//    }
    
    
    [cell.contentLab addCorner:2];
    if (mm.isSel) {
        cell.contentLab.backgroundColor = [UIColor colorWithHexString:@"#EBF0FE"];
        cell.contentLab.textColor = ColorWith3C6CFC;
    }
    else {
        cell.contentLab.backgroundColor = ColorWithF5F5F5;
        cell.contentLab.textColor = ColorWith333333;
    }
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LXFindCarModel *model = self.data[indexPath.section];
    [model.child setValue:@NO forKeyPath:@"isSel"];
    
 
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return CGSizeMake(ScreenWidth , 50);
    }
    else if (indexPath.section == 2&&self.isSelOilCar) {
        return CGSizeMake(0.0001, 0.0001);
    }
    return CGSizeMake((ScreenWidth - 5) / 4, 40);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 2&&self.isSelOilCar) {
        return CGSizeMake(0.0001, 0.0001);
    }
    return CGSizeMake((ScreenWidth - 5) / 4, 40);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *headerV = (UICollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class]) forIndexPath:indexPath];
        [headerV setClipsToBounds:YES];
        
        for (UIView *v in headerV.subviews) {
            [v removeFromSuperview];
        }
        LXFindCarModel *model = self.data[indexPath.section];
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, ScreenWidth, headerV.height-5)];
        lab.text = model.title;
        lab.textColor = [UIColor colorWithHexString:@"#222222"];
        lab.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        [lab addAttributes:@{NSForegroundColorAttributeName:ColorWithAAAAAA} forString:@"(公里)" size:12];
        [headerV addSubview:lab];
        return headerV;
    }
    
    return nil;
    
}
-(void)submitAction{
    
    LXCarLeaseApplyViewController *vc = [[LXCarLeaseApplyViewController alloc] init];
    vc.fromVc = self.fromVc;
    vc.carModel = self.model;
    vc.type = 3;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
